home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Tools / Turbo Pascal V7 / DEMOS.ZIP / DIRDEMO.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1992-11-03  |  4.9 KB  |  244 lines

  1. {************************************************}
  2. {                                                }
  3. { Turbo Directory Demo                           }
  4. { Copyright (c) 1985,90 by Borland International }
  5. {                                                }
  6. {************************************************}
  7.  
  8. program DirDemo;
  9. { Demonstration program that shows how to use:
  10.  
  11.     o Directory routines from DOS unit
  12.     o Procedural types (used by QuickSort)
  13.  
  14.   Usage:
  15.  
  16.     dirdemo [options] [directory mask]
  17.  
  18.   Options:
  19.  
  20.     -W      Wide display
  21.     -N      Sort by file name
  22.     -S      Sort by file size
  23.     -T      Sort by file date and time
  24.  
  25.   Directory mask:
  26.  
  27.     Path, Filename, wildcards, etc.
  28.  
  29. }
  30.  
  31. {$I-,S-}
  32. {$M 8192,8192,655360}
  33.  
  34. uses Dos;
  35.  
  36. const
  37.   MaxDirSize = 512;
  38.   MonthStr: array[1..12] of string[3] = (
  39.     'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
  40.     'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec');
  41.  
  42. type
  43.   DirPtr   = ^DirRec;
  44.   DirRec   = record
  45.                Attr: Byte;
  46.                Time: Longint;
  47.                Size: Longint;
  48.                Name: string[12];
  49.              end;
  50.   DirList  = array[0..MaxDirSize - 1] of DirPtr;
  51.   LessFunc = function(X, Y: DirPtr): Boolean;
  52.  
  53. var
  54.   WideDir: Boolean;
  55.   Count: Integer;
  56.   Less: LessFunc;
  57.   Path: PathStr;
  58.   Dir: DirList;
  59.  
  60. function NumStr(N, D: Integer): String;
  61. begin
  62.   NumStr[0] := Chr(D);
  63.   while D > 0 do
  64.   begin
  65.     NumStr[D] := Chr(N mod 10 + Ord('0'));
  66.     N := N div 10;
  67.     Dec(D);
  68.   end;
  69. end;
  70.  
  71. {$F+}
  72.  
  73. function LessName(X, Y: DirPtr): Boolean;
  74. begin
  75.   LessName := X^.Name < Y^.Name;
  76. end;
  77.  
  78. function LessSize(X, Y: DirPtr): Boolean;
  79. begin
  80.   LessSize := X^.Size < Y^.Size;
  81. end;
  82.  
  83. function LessTime(X, Y: DirPtr): Boolean;
  84. begin
  85.   LessTime := X^.Time > Y^.Time;
  86. end;
  87.  
  88. {$F-}
  89.  
  90. procedure QuickSort(L, R: Integer);
  91. var
  92.   I, J: Integer;
  93.   X, Y: DirPtr;
  94. begin
  95.   I := L;
  96.   J := R;
  97.   X := Dir[(L + R) div 2];
  98.   repeat
  99.     while Less(Dir[I], X) do Inc(I);
  100.     while Less(X, Dir[J]) do Dec(J);
  101.     if I <= J then
  102.     begin
  103.       Y := Dir[I];
  104.       Dir[I] := Dir[J];
  105.       Dir[J] := Y;
  106.       Inc(I);
  107.       Dec(J);
  108.     end;
  109.   until I > J;
  110.   if L < J then QuickSort(L, J);
  111.   if I < R then QuickSort(I, R);
  112. end;
  113.  
  114. procedure GetCommand;
  115. var
  116.   I,J: Integer;
  117.   Attr: Word;
  118.   S: PathStr;
  119.   D: DirStr;
  120.   N: NameStr;
  121.   E: ExtStr;
  122.   F: File;
  123. begin
  124.   WideDir := False;
  125.   @Less := nil;
  126.   Path := '';
  127.   for I := 1 to ParamCount do
  128.   begin
  129.     S := ParamStr(I);
  130.     if S[1] = '-' then
  131.       for J := 2 to Length(S) do
  132.         case UpCase(S[J]) of
  133.           'N': Less := LessName;
  134.           'S': Less := LessSize;
  135.           'T': Less := LessTime;
  136.           'W': WideDir := True;
  137.         else
  138.           WriteLn('Invalid option: ', S[J]);
  139.           Halt(1);
  140.         end
  141.     else
  142.       Path := S;
  143.   end;
  144.   Path := FExpand(Path);
  145.   if Path[Length(Path)] <> '\' then
  146.   begin
  147.     Assign(F, Path);
  148.     GetFAttr(F, Attr);
  149.     if (DosError = 0) and (Attr and Directory <> 0) then
  150.       Path := Path + '\';
  151.   end;
  152.   FSplit(Path, D, N, E);
  153.   if N = '' then N := '*';
  154.   if E = '' then E := '.*';
  155.   Path := D + N + E;
  156. end;
  157.  
  158. procedure FindFiles;
  159. var
  160.   F: SearchRec;
  161. begin
  162.   Count := 0;
  163.   FindFirst(Path, ReadOnly + Directory + Archive, F);
  164.   while (DosError = 0) and (Count < MaxDirSize) do
  165.   begin
  166.     GetMem(Dir[Count], Length(F.Name) + 10);
  167.     Move(F.Attr, Dir[Count]^, Length(F.Name) + 10);
  168.     Inc(Count);
  169.     FindNext(F);
  170.   end;
  171. end;
  172.  
  173. procedure SortFiles;
  174. begin
  175.   if (Count <> 0) and (@Less <> nil) then
  176.     QuickSort(0, Count - 1);
  177. end;
  178.  
  179. procedure PrintFiles;
  180. var
  181.   I, P: Integer;
  182.   Total: Longint;
  183.   T: DateTime;
  184.   N: NameStr;
  185.   E: ExtStr;
  186. begin
  187.   WriteLn('Directory of ', Path);
  188.   if Count = 0 then
  189.   begin
  190.     WriteLn('No matching files');
  191.     Exit;
  192.   end;
  193.   Total := 0;
  194.   for I := 0 to Count-1 do
  195.   with Dir[I]^ do
  196.   begin
  197.     P := Pos('.', Name);
  198.     if P > 1 then
  199.     begin
  200.       N := Copy(Name, 1, P - 1);
  201.       E := Copy(Name, P + 1, 3);
  202.     end else
  203.     begin
  204.       N := Name;
  205.       E := '';
  206.     end;
  207.     Write(N, ' ': 9 - Length(N), E, ' ': 4 - Length(E));
  208.     if WideDir then
  209.     begin
  210.       if Attr and Directory <> 0 then
  211.         Write(' DIR')
  212.       else
  213.         Write((Size + 1023) shr 10: 3, 'k');
  214.       if I and 3 <> 3 then
  215.         Write(' ': 3)
  216.       else
  217.         WriteLn;
  218.     end else
  219.     begin
  220.       if Attr and Directory <> 0 then
  221.         Write('<DIR>   ')
  222.       else
  223.         Write(Size: 8);
  224.       UnpackTime(Time, T);
  225.       WriteLn(T.Day: 4, '-',
  226.         MonthStr[T.Month], '-',
  227.         NumStr(T.Year mod 100, 2),
  228.         T.Hour: 4, ':',
  229.         NumStr(T.Min, 2));
  230.     end;
  231.     Inc(Total, Size);
  232.   end;
  233.   if WideDir and (Count and 3 <> 0) then WriteLn;
  234.   WriteLn(Count, ' files, ', Total, ' bytes, ',
  235.     DiskFree(Ord(Path[1])-64), ' bytes free');
  236. end;
  237.  
  238. begin
  239.   GetCommand;
  240.   FindFiles;
  241.   SortFiles;
  242.   PrintFiles;
  243. end.
  244.